package br.com.pguide.activity;

import br.com.pguide.R;
import br.com.pguide.estrutura.Perfil;
import br.com.pguide.parser.Parser;
import br.com.pguide.utils.Mensagem;
import br.com.pguide.webservice.WebService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

public class MeuPerfilActivity extends Activity {

	private String username;
	private double latitude;
	private double longitude;
	private String deviceName;
	private String manufacturerName;
	private Perfil perfil;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent it = getIntent();
		this.username = it.getStringExtra("username");
		
		setContentView(R.layout.perfil);
		getContentFromContext();
		setContent();
	}
	
	public void alterar_dados_event (View view) {
		Mensagem.exibirMensagem("nao feito - dados", this);
	}
	
	public void alterar_senha_event (View view) {
		Mensagem.exibirMensagem("nao feito - senha", this);
	}
	
	protected void setContent() {
		try {
			((TextView) findViewById(R.id.text_nome)).setText(perfil.getNome());
			((TextView) findViewById(R.id.text_sobrenome)).setText(perfil.getSobrenome());
			((TextView) findViewById(R.id.text_email)).setText(perfil.getEmail());
			((TextView) findViewById(R.id.text_cidade)).setText(perfil.getCidade());
			((TextView) findViewById(R.id.text_estado)).setText(perfil.getEstado());
			((TextView) findViewById(R.id.text_latitude)).setText("Lat: " + String.valueOf(latitude));
			((TextView) findViewById(R.id.text_longitude)).setText("Long: " + String.valueOf(longitude));
			((TextView) findViewById(R.id.text_dispositivo)).setText(String.valueOf(deviceName));
			((TextView) findViewById(R.id.text_fabricante)).setText(String.valueOf(manufacturerName));
		} catch (Exception e) {
			Mensagem.exibirMensagem(e.getMessage().toString(), this);
		}
	}
	
	protected void getContentFromContext() {
		getDeviceName();
		getManufacturerName();
		getLocation();
		getProfileInfo();
	}
	
	protected void getProfileInfo() {
		String str_perfil = WebService.get_perfil(username);
		this.perfil = Parser.get_perfil(str_perfil);
	}
	
	protected void getDeviceName() {
		this.deviceName = android.os.Build.MODEL.toString();
	}
	
	protected void getManufacturerName() {
		this.manufacturerName = android.os.Build.MANUFACTURER.toString();
	}
	
	protected void getLocation() {
		if (deviceName.equals("sdk")) {
			// modo de desenvolvimento - sdk
			this.latitude = -9.4157852;
			this.longitude = -36.6223826;
			return;
		}
		
		habilitarGPS();
		// Get the location manager
		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// Define the criteria how to select the locatioin provider -> use
		// default
		Criteria criteria = new Criteria();
		String provider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(provider);
		if (location != null) {
			this.latitude = (double) (location.getLatitude());
			this.longitude = (double) (location.getLongitude());
		} else {
			Mensagem.exibirMensagem("Latitude: " + "is not available" + 
					"\nLongitude: " + "is not available", this);
		}
	}
	
	public void habilitarGPS() {
		// a classe LocationManager provê o acesso ao serviço de localização do Android
		LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
		boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
		
		if (!enabled) {
			/* o GPS não pode ser habilitado diretamente em código
			** o usuário precisa autorizar a sua habilitção 
			** apenas a Activity do Google Map habilita automaticamente o GPS */
			Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			startActivity(intent);
		}
	}
}
